package com.ry;

import java.util.Calendar;

import com.ry.util.timepick.DateSlider;
import com.ry.util.timepick.DateTimeSlider;
import com.ry.util.timepick.R;
import com.ry.util.timepick.labeler.TimeLabeler;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class UseCarInformationActivity extends Activity {
	private Button btnBack;
	private Button btnNext;
	private TextView tv_when;
	private TextView txtTime;
	private TextView txtBoardingLocation;
	private TextView txtAlightingLoacation;

	private RelativeLayout rlBooktime;
	private RelativeLayout rlBookduration;
	private RelativeLayout rlBoarding_location;
	private RelativeLayout rlAlighting_location;
	
	private static final int DATETIMESELECTOR_ID = 1;

	private String[] timeDuration = new String[] { "1小时", "2小时", "3小时", "4小时", "5小时", "6小时", "7小时", "8小时", "9小时",
			"10小时", "11小时", "12小时" };

	private String[] location = new String[] { "西安一号大街", "西安二号大街", "西安三号大街", "西安四号大街", "西安五号大街" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.use_car_information);
		init();
	}

	private void init() {
		btnBack = (Button) findViewById(R.use_car_information.btn_back);
		btnNext = (Button) findViewById(R.use_car_information.btn_next);
		
		tv_when = (TextView) findViewById(R.use_car_information.tv_when);
		txtTime = (TextView) findViewById(R.use_car_information.txt_time);
		txtBoardingLocation = (TextView) findViewById(R.use_car_information.txt_boarding_location);
		txtAlightingLoacation = (TextView) findViewById(R.use_car_information.txt_alighting_location);

		rlBooktime = (RelativeLayout) findViewById(R.id.rl_booktime);
		rlBookduration = (RelativeLayout) findViewById(R.id.rl_bookduration);
		rlBoarding_location = (RelativeLayout) findViewById(R.id.rl_boarding_location);
		rlAlighting_location = (RelativeLayout) findViewById(R.id.rl_alighting_location);

		rlBooktime.setOnClickListener(booktimeOnClickListener);
		rlBookduration.setOnClickListener(bookdurationOnClickListener);
		rlBoarding_location.setOnClickListener(boarding_locationOnClickListener);
		rlAlighting_location.setOnClickListener(alighting_locationOnClickListener);


		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		btnNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if("请选择用车时间".equals(tv_when.getText().toString().trim())){
					Toast.makeText(UseCarInformationActivity.this, "请输入用车时间。", Toast.LENGTH_SHORT).show();
					return;
				}
				if("请选择用车时长".equals(txtTime.getText().toString().trim())){
					Toast.makeText(UseCarInformationActivity.this, "请输入用车时长。", Toast.LENGTH_SHORT).show();
					return;
				}
				if("请选择上车地点".equals(txtBoardingLocation.getText().toString().trim())){
					Toast.makeText(UseCarInformationActivity.this, "请输入上车地点。", Toast.LENGTH_SHORT).show();
					return;
				}
				if("请选择下车地点".equals(txtAlightingLoacation.getText().toString().trim())){
					Toast.makeText(UseCarInformationActivity.this, "请输入下车地点。", Toast.LENGTH_SHORT).show();
					return;
				}
				Toast.makeText(UseCarInformationActivity.this, "保存成功。", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(UseCarInformationActivity.this, OrderInformaionActivity.class);
				startActivity(intent);
			}
		});
	}
	
	
	    
    @Override
    protected Dialog onCreateDialog(int id) {
    	final Calendar c = Calendar.getInstance();
    	return new DateTimeSlider(this,mDateTimeSetListener,c);
    }
    
    private DateSlider.OnDateSetListener mDateTimeSetListener =
        new DateSlider.OnDateSetListener() {
            public void onDateSet(DateSlider view, Calendar selectedDate) {
                // update the dateText view with the corresponding date
                int minute = selectedDate.get(Calendar.MINUTE) /
                        TimeLabeler.MINUTEINTERVAL*TimeLabeler.MINUTEINTERVAL;
                tv_when.setText(String.format("%tY年%tm月%te日 %tH:%02d",
                        selectedDate, selectedDate, selectedDate, selectedDate, minute));
            }
	 };
	
	private OnClickListener booktimeOnClickListener = new OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showDialog(DATETIMESELECTOR_ID);
			
			
			
			
			//com.ry.util.timepick.DateTimeDialogActivity
		}
	};
	
	private OnClickListener bookdurationOnClickListener = new OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog();
		}
	};
	
	private OnClickListener boarding_locationOnClickListener = new OnClickListener() {
		public void onClick(View v) {
			AlertDialog.Builder builder = new AlertDialog.Builder(UseCarInformationActivity.this);
			builder.setTitle("选择上车地点");
			builder.setItems(location, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					txtBoardingLocation.setText(location[which]);
				}
			});
			builder.show();

		}
	};
	
	private OnClickListener alighting_locationOnClickListener = new OnClickListener() {
		public void onClick(View v) {
			AlertDialog.Builder builder = new AlertDialog.Builder(UseCarInformationActivity.this);
			builder.setTitle("选择下车地点");
			builder.setItems(location, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					txtAlightingLoacation.setText(location[which]);
				}
			});
			builder.show();

		}
	};
	
	private DialogInterface.OnClickListener timeDurationOnClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface arg0, int which) {
			txtTime.setText(timeDuration[which]);
		}
	};
	
	
	protected void dialog(){
		 AlertDialog.Builder builder = new AlertDialog.Builder(this);
		 builder.setTitle("选择用车时长");
		 builder.setItems(timeDuration, timeDurationOnClickListener);
		 builder.show();
		
	}
	
	

}
